package com.china08.yxyapi.api.manage.tec.office.classnotice;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass.YxyNotifictionUser;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyNoticeClassRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 老师 班级通知 新增
 * 
 */
@CrossOrigin
@RestController("TecClassnoticeCrtController")
public class ClassnoticeCrtController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeClassRepository yxyNoticeClassRepository;

    @Autowired
    private RestTemplate restTemplate;

    @Value(value = "${yx.rest.api.yx.path}")
    private String yxyApiYxUrl;

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/api_yw/manage/tec/office/classnotice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        RespModel respModel = new RespModel();
        YxyProfile yxyProfile = getProfile(request, response);
        String currentUserId = yxyProfile.getId();
        String schoolId = yxyProfile.getSchoolId();
        String userNick = yxyProfile.getUserNick();
        String avatar = yxyProfile.getUserAvator();

        List<YxyNoticeClass> yxyNoticeClassLst = new ArrayList<YxyNoticeClass>();
        // 查询班级信息
        List<YxyNotifictionUser> receiver = null;
        YxyNotifictionUser yxyNotifictionUser = null;
        // 学生状态为在校状态
        Map<String, List<Map<String, String>>> yxyStudentLstsMap = null;

        List<String> classIdLst = reqModel.classIdLst;
        List<String> classNameLst = reqModel.classNameLst;
        YxyNoticeClass yxyNoticeClass = null;
        for (int i = 0; i < classIdLst.size(); i++) {
            receiver = new ArrayList<YxyNotifictionUser>();
            yxyNoticeClass = new YxyNoticeClass();
            yxyNoticeClass.setSchoolId(schoolId);
            yxyNoticeClass.setTitle(reqModel.title);
            yxyNoticeClass.setContent(reqModel.content);
            yxyNoticeClass.setAttachmentName(reqModel.attachmentName);
            yxyNoticeClass.setAttachmentPath(reqModel.attachmentPath);

            yxyNoticeClass.setClassId(classIdLst.get(i));
            yxyNoticeClass.setSchoolId(schoolId);
            // 查询班级名称
            yxyNoticeClass.setClassNick(classNameLst.get(i));

            yxyNoticeClass.setImage(reqModel.image);
            yxyNoticeClass.setUserId(currentUserId);
            yxyNoticeClass.setNickName(userNick);
            yxyNoticeClass.setAvatar(avatar);
            // 查询班级内的所有班级成员信息
            // FIXED 云校库
            yxyStudentLstsMap = restTemplate.getForObject(yxyApiYxUrl + "/comm/classStudent/schoolId/" + schoolId
                    + "/getStudentLstByClassId/" + classIdLst.get(i), Map.class);
            List<Map<String, String>> list = yxyStudentLstsMap.get("list");
            for (Map<String, String> map : list) {
                yxyNotifictionUser = new YxyNoticeClass().new YxyNotifictionUser();
                yxyNotifictionUser.setUserId(map.get("id"));
                yxyNotifictionUser.setUserNick(map.get("name"));
                yxyNotifictionUser.setIsRead(0);
                receiver.add(yxyNotifictionUser);
            }
            yxyNoticeClass.setReceiver(receiver);
            yxyNoticeClassLst.add(yxyNoticeClass);
        }
        // FIXED 业务库
        yxyNoticeClassRepository.save(yxyNoticeClassLst);
        respModel.setMessage("保存成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        // 班级id list
        public List<String> classIdLst;
        // 班级name list
        public List<String> classNameLst;
        // 标题
        public String title;
        // 内容
        public String content;
        // 图片列表
        public List<String> image;
        // 附件名
        public String attachmentName;
        // 附件路径
        public String attachmentPath;

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
